package br.com.plurianual.util.datamodel;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.primefaces.model.SortOrder;

import br.com.plurianual.controller.SearchHelper;
import br.com.plurianual.exception.DataAccessLayerException;
import br.com.plurianual.model.dao.DAO;
import br.com.plurianual.model.entity.BaseEntity;
import br.com.plurianual.util.FacesUtil;
import br.com.plurianual.util.search.SearchDTO;
import br.com.plurianual.util.search.SearchFilter;
import br.com.plurianual.util.search.SearchFilterType;
import br.com.plurianual.util.search.SearchOrder;

/**
 * DataModel genrico para listagen de entidades
 * 
 * @author FRAGA
 *
 */
public class SimpleFilterLazyDataModel extends GenericLazyDataModel {

        private static final long serialVersionUID = 289329139064417236L;
        
        private String groupFilterKey;
        
        /**
         *  Construtor padrão
         */
        public SimpleFilterLazyDataModel() {
        }

        

        /**
         * Cria uma intancia e atribui o objeto DAO que será responsável pela listagem.
         * O parametro groupFilterKey define a chave onde será guardado o 
         * 
         * @param dao
         * @param filtersKey
         */
        @SuppressWarnings("rawtypes")
        public SimpleFilterLazyDataModel(DAO dao, String groupFilterKey) {
                super(dao);
                this.setGroupFilterKey(groupFilterKey);
                
        }

        /* (non-Javadoc)
         * @see org.primefaces.model.LazyDataModel#load(int, int, java.lang.String, org.primefaces.model.SortOrder, java.util.Map)
         */
        @SuppressWarnings({ "unchecked", "rawtypes" })
        @Override
        public List load(int first, int pageSize, String sortField,
                        SortOrder sortOrder, Map<String, String> filters) {
                        System.out.println("load data model");
                
                
                List<BaseEntity> list = new ArrayList<BaseEntity>();
                SearchDTO search = new SearchDTO();
                
                
                SearchHelper searchHelper = (SearchHelper) FacesUtil.getSessionMenagedBean("searchHelper");
                List<SearchFilter> filtersList = new ArrayList<SearchFilter>();
                
                if(searchHelper.getFiltersMap().containsKey(this.getGroupFilterKey())){
                Map<String, SearchFilter> mapFilters = searchHelper.getFiltersMap();
                
                        for(String key : mapFilters.keySet()){
                                String value = (String)mapFilters.get(key).getValue();
                                if(!StringUtils.isBlank(value)){
                                        mapFilters.get(key).setPropertyName(key);
                                        mapFilters.get(key).setOperator(SearchFilterType.STARTS_WITH);
                                        filtersList.add(mapFilters.get(key));
                                }
                        }
                }
                
                if(!filtersList.isEmpty()){
                        search.getFilters().addAll(filtersList);
                }
                search.setFirstResult(first);
                search.setMaxResult(pageSize);
                search.setOrders(new ArrayList<SearchOrder>());

                search.getOrders().add(this.buildSort(sortField, sortOrder));
                
                

                try {

                        int totalRows = dao.countAllByFilter(search.getFilters());
                        this.setRowCount(totalRows);
                        list = (List<BaseEntity>) dao.findBySearchDTO(search);
                        // this.setPageSize(10);

                } catch (DataAccessLayerException e) {
                        e.printStackTrace();
                }

                return list;
                
        }


        /**
         * @return the groupFilterKey
         */
        public String getGroupFilterKey() {
                return groupFilterKey;
        }


        /**
         * @param groupFilterKey the groupFilterKey to set
         */
        public void setGroupFilterKey(String groupFilterKey) {
                this.groupFilterKey = groupFilterKey;
        }



}
